
const express = require('express');
const cors = require('cors');
const router = require('./router');
const bodyParser = require('body-parser');

const app = express();

//express项目中通常使用body - parser进行post参数的解析，最常用的是其中的json和urlencoded的parser，
//可分别对以JSON格式的post参数和urlencoeded的post参数进行解析，均可获得一个JSON化的req.body，
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());


//解决跨域问题
app.all("*", function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OP0TIONS");
  res.header("X-Powered-By", "3.2.1");
  res.header("Content-Type", "application/json;charset=utf-8");
  next();
});

//use路由
app.use(router);

//run at port 
app.listen(8080, () => {
  console.log('run-----');
})
